<?php
/**
* @version $Id: news.php 10002 2008-02-08 10:56:57Z willebil $
* @package Joomla
* @subpackage news
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

require_once( $mainframe->getPath( 'front_html', 'com_content' ) );


$id 		= intval( mosGetParam( $_REQUEST ,'id', 0 ) );
$catid 		= intval( mosGetParam( $_REQUEST ,'catid', 0 ) );
$sectionid	= intval( mosGetParam( $_REQUEST ,'sectionid', 0 ) );
$productID	= intval( mosGetParam( $_REQUEST ,'productID', 0 ) );

switch( $task ) {
	case 'view':
		viewDisplay( $id, $catid );
		break;
	case 'blogsection':	
	case 'blogcategory':
	case 'hangsx':
		showList( $id );
		break;
	case 'download':
		downloadQuotes();
		break;
	case 'save':
		saveQuotes($catid);
		break;		
	
	default:
		showList( $id );
		break;	
}

//Function showList
function showList( $id ){
	global $mosConfig_list_limit, $database, $mosConfig_live_site, $mainframe;
	$limit	= intval( mosGetParam( $_GET, 'limit', $mosConfig_list_limit ) );
	$limit	= ( $limit ? $limit : $mosConfig_list_limit );
	$limitstart = intval( mosGetParam( $_GET, 'limitstart', 0 ) );
	
	$task 	= mosGetParam($_REQUEST, 'task', '');
	if ($task=='blogsection'){
		$where = " AND cd.sectionid=".$id;
		$sql = "select * from #__sections where id=".$id;
		$database->setQuery($sql);
		$database->loadObject($row);
		
		$title = $row->title;
	}
	if ($task=='blogcategory'){
		$sql = "select * from #__categories where id=".$id;
		$database->setQuery($sql);
		$database->loadObject($row);
		
		$sql = "select * from #__categories where parent_id=".$id;
		$database->setQuery($sql);
		$lsRows = $database->loadObjectList();
		if (count($lsRows)>0){
			$arr = "(".$id.",";
			for ($i=0; $i<count($lsRows); $i++){
				if ($i==(count($lsRows)-1)){
					$str = "";
				}else{
					$str = ",";
				}
				$arr .= $lsRows[$i]->id.$str;
			}
			$arr .= ")";
			//echo $arr;
			$where = " AND cd.catid IN".$arr;
		}else{
			$where = " AND cd.catid=".$id;
		}
		
		$title = $row->title;
	}
	if ($task=='hangsx'){
		$where = " AND catphone_id=".$id;
		$sql = "select * from #__catphone where id=".$id;
		$database->setQuery($sql);
		$database->loadObject($row);
		$title = $row->name;
	}
	
	$numberPage = 10;
	
	$mainframe->setPageTitle($title);
			
	$sql = "SELECT COUNT(*) FROM #__content AS cd, #__categories AS cat, #__sections AS sec WHERE cd.state=1 AND cd.catid=cat.id AND cd.sectionid=sec.id".$where;
	$database->setQuery($sql);
	$total = $database->loadResult();
	$sql = "SELECT cd.id AS id, cd.catid AS catid, cd.sectionid AS sectionid, cd.title AS title, cd.title_alias AS title_alias, cd.created AS created, cd.thumbnailImage AS thumbnailImage, cd.mediumImage AS mediumImage, cd.largeImage AS largeImage, "
					."cat.title AS titlecat, cat.name AS html_namecat, "
					."sec.title AS title_sec, sec.name AS html_secname"
			."\n FROM #__content AS cd, #__categories AS cat, #__sections AS sec"
			."\n WHERE cd.state=1 AND cd.catid=cat.id AND cd.sectionid=sec.id".$where." AND cd.vip=0 AND cd.hits=0 ORDER BY cd.ordering ASC, cd.id DESC";
	$database->setQuery( $sql, $limitstart, $numberPage );
	$rows = $database->loadObjectlist();
	
	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $numberPage );
	$link_page = sefRelToAbs($mosConfig_live_site."/index.php?option=com_content&task=".$task."&id=".$id);

	HTML_content::showListProduct( $title, $total, $rows, $pageNav, $link_page, $numberPage, $row );	
}

//Detail product
function viewDisplay( $id, $catid ) {
	global $database, $mosConfig_live_site, $mainframe;
	$limit	= intval( mosGetParam( $_GET, 'limit', $mosConfig_list_limit ) );
	$limit	= ( $limit ? $limit : $mosConfig_list_limit );
	$limitstart = intval( mosGetParam( $_GET, 'limitstart', 0 ) );		
	
	//Get product
	$sql = "SELECT * FROM #__content WHERE id=".$id;
	$database->setQuery($sql);
	$database->loadObject($row);
	
	//Get User
	$sql = "SELECT user_class FROM #__users WHERE id=".$row->user_id;
	$database->setQuery($sql);
	$database->loadObject($thisUser);
	if ($thisUser->user_class == 1){ //Nha cung cap san pham
		
	}else{ //Thanh vien bt
		
	}
	
	$mainframe->setPageTitle($row->title);
	
	$numberPage = 10;
		
	$sql = "SELECT COUNT(*) FROM #__content WHERE id!=".$id." AND state=1 AND catid=".$row->catid;
	$database->setQuery($sql);
	$total = $database->loadResult();	
	$sql = "SELECT * FROM #__content WHERE id!=".$id." AND state=1 AND catid=".$row->catid." ORDER BY ordering ASC, id DESC";
	$database->setQuery( $sql, $limitstart, $numberPage );
	$rows = $database->loadObjectlist();
	
	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $numberPage );
	$link_page = sefRelToAbs($mosConfig_live_site."/index.php?option=com_content&task=view&catid=".$row->catid."&id=".$id);
	
	if ($row->sectionid==1){
		$sql = "SELECT * FROM #__catphone WHERE id=".$row->catphone_id;
		$database->setQuery($sql);
		$database->loadObject($hangsx);
		
		$linkhangsx = sefRelToAbs("index.php?option=com_content&task=hangsx&id=".$hangsx->id);
		//count hangsx
		$sql = "SELECT COUNT(*) FROM #__content WHERE catphone_id=".$hangsx->id." AND state=1";
		$database->setQuery($sql);
		$totalhangsx = $database->loadResult();
		
		HTML_content::viewDisplayProduct( $row, $total, $rows, $pageNav, $link_page, $numberPage, $hangsx, $totalhangsx, $linkhangsx );
	}else{
		HTML_content::viewDisplayNews( $row, $total, $rows, $pageNav, $link_page, $numberPage );
	}		
}

//Tải báo giá
function downloadQuotes(){
	global $database, $mosConfig_live_site, $mainframe;
	$mainframe->setPageTitle('Tải báo giá');
	
	$sql = "SELECT * FROM #__categories WHERE section=1 AND published=1 AND parent_id=0 ORDER BY ordering ASC, id DESC";
	$database->setQuery($sql);
	$lsCategories = $database->loadOBjectList();
	
	HTML_content::downloadQuotes( $lsCategories );
}

//Save báo giá
function saveQuotes($catid){
	global $database, $mosConfig_absolute_path;
	// load library
	require $mosConfig_absolute_path.'/includes/excel/php-excel.class.php';
	
	$sql = "select name from #__categories where id=".$catid;
	$database->setQuery($sql);
	$database->loadObject($name);
	
	$sql = "select * from #__categories where parent_id=".$catid;
	$database->setQuery($sql);
	$lsRows = $database->loadObjectList();
	if (count($lsRows)>0){
		$array = "(".$catid.",";
		for ($j=0; $j<count($lsRows); $j++){
			if ($j==(count($lsRows)-1)){
				$str = "";
			}else{
				$str = ",";
			}
			$array .= $lsRows[$j]->id.$str;
		}
		$array .= ")";
		//echo $array;
		$where = " AND catid IN".$array;
	}else{
		$where = " AND catid=".$catid;
	}
	
	$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND sectionid=1".$where;
	$database->setQuery($sql);
	$total = $database->loadResult();
	$sql = "SELECT * FROM #__content WHERE state=1 AND sectionid=1".$where." ORDER BY ordering ASC, id DESC";
	$database->setQuery($sql);
	$rows = $database->loadObjectList();

	if ($name->name!=''){
		$filename="$name->name";
	}else{
		$filename="excelreport";
	}
    
    // Send Header
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=$filename.xls");
    header("Content-Transfer-Encoding: binary");

    // XLS Data Cell
    xlsBOF();
    xlsWriteLabel(1,0,"Báo giá sản phẩm VNIG");
    xlsWriteLabel(2,0,"CODE : ");
    xlsWriteLabel(2,1,"VNIG");
    xlsWriteLabel(3,0,"TIEU DE : ");
    xlsWriteLabel(3,1,$filename);
    xlsWriteLabel(4,0,"Nhom : ");
    xlsWriteLabel(4,1,$filename);
    xlsWriteLabel(6,0,"STT");
    xlsWriteLabel(6,1,"MA SAN PHAM");
    xlsWriteLabel(6,2,"TEN SAN PHAM");
    xlsWriteLabel(6,3,"GIA BAN");
    xlsWriteLabel(6,4,"GIA GIAM");
    $xlsRow = 7;
	for ($i=0; $i<count($rows); $i++){
		if ($rows[$i]->price_giam>0){
			$price_giam = number_format($rows[$i]->price_giam,0,",",".")." VND";
		}else{
			$price_giam = "0";
		}
        xlsWriteNumber($xlsRow,0,$i+1);
        xlsWriteNumber($xlsRow,1,$rows[$i]->code);
        xlsWriteLabel($xlsRow,2,$rows[$i]->title_alias);
        xlsWriteLabel($xlsRow,3,number_format($rows[$i]->price,0,",",".")." VND");
        xlsWriteLabel($xlsRow,4,$price_giam);
        $xlsRow++;
    }
    xlsEOF();
    exit();
}
?>